package com.diandian.backend.jingdong.home.exchange.service;

import com.diandian.backend.jingdong.home.exchange.client.DianDianStoreClient;
import com.diandian.backend.jingdong.home.exchange.client.dto.JingdongHomeStoreDetailOpenApiParamDTO;
import com.diandian.backend.jingdong.home.exchange.client.dto.StoreJingdongHomeStoreInfoDetailResp;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.BaseResult;
import com.diandian.jingdong.home.boot.starter.service.product.JingdongProductQueryBasicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BusinessDataTransferService {

    @Autowired
    private DianDianStoreClient dianDianStoreClient;

    @Autowired
    private JingdongProductQueryBasicService productQueryBasicService;

    public String offlineCodeToOnlineCode(String belong, String offlineStoreCode){

        JingdongHomeStoreDetailOpenApiParamDTO param = new JingdongHomeStoreDetailOpenApiParamDTO();
        param.setBelong(belong);
        param.setOfflineCode(offlineStoreCode);

        BaseResult<StoreJingdongHomeStoreInfoDetailResp> baseResult = dianDianStoreClient.getJingdongStoreInfo(param);
        if(baseResult == null || baseResult.getCode() != 0){
            throw new DefaultException("获取京东门店信息异常");
        }
        if(baseResult.getData() == null){
            throw new DefaultException("未获取到京东门店");
        }

        StoreJingdongHomeStoreInfoDetailResp storeDetail = baseResult.getData();
        if(storeDetail == null || storeDetail.getOnlineCode() == null || "".equals(storeDetail.getOnlineCode())){
            throw new DefaultException("未获取到京东门店");
        }

        return storeDetail.getOnlineCode();
    }

    public Long localSkuIdToDjSkuId(String appKey, String appSecret, String token, String onlineStoreCode, String localSkuId) throws Exception {
        return productQueryBasicService.getSkuIdByOuterSkuIds(appKey, appSecret, token, null, onlineStoreCode, localSkuId);
    }

}
